<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "Recruit".
 */
class Recruit extends ActiveRecord {

    // 状态
    // @describe 状态流转图
    // @describe 待审核 -> 招聘中 -> 招聘成功/中止
    const StatusExamine = 0;
    const StatusRelease = 1;
    const StatusSuccess = 11;
    const StatusAbort = 12;
    public static $statusSelector = [
        self::StatusExamine => ['title' => '待审核', 'status' => 'gray'],
        self::StatusRelease => ['title' => '招聘中', 'status' => 'orange'],
        self::StatusSuccess => ['title' => '已完成', 'status' => 'green'],
        self::StatusAbort => ['title' => '中止', 'status' => 'red'],
    ];

    // 雇佣类型
    const TypeCustomized = 1;
    const TypeFullTime = 2;
    public static $typeSelector = [
        self::TypeCustomized => ['title' => '全职', 'status' => 'blue'],
        self::TypeFullTime => ['title' => '定制', 'status' => 'purple'],
    ];

    // 雇佣类型
    const OnlineOff = 0;
    const OnlineOn = 1;
    public static $onlineSelector = [
        self::OnlineOff => ['title' => '下架', 'status' => 'gray'],
        self::OnlineOn => ['title' => '上架', 'status' => 'blue'],
    ];

    // 佣金档次
    const BudgetFirst = 0;
    const BudgetSecond = 1;
    const BudgetThird = 2;
    const BudgetFourth = 3;
    const BudgetFifth = 4;
    public static $budgetSelector = [
        self::BudgetFirst => ['title' => '1000元以下', 'status' => 'gray'],
        self::BudgetSecond => ['title' => '1000~2999元', 'status' => 'orange'],
        self::BudgetThird => ['title' => '3000~5999元', 'status' => 'blue'],
        self::BudgetFourth => ['title' => '6000~9999元', 'status' => 'purple'],
        self::BudgetFifth => ['title' => '10000元以上', 'status' => 'red'],
    ];

    // only define rules for those attributes that
    // will receive user inputs.
    public function rules()
    {
        return [
            [['title', 'employer_id', 'categories', 'platforms', 'type', 'budget', 'online', 'website'], 'required'],
            [['employer_id', 'type', 'budget', 'status', 'view', 'online', 'collection', 'deleted_at'], 'integer'],
            [['title'], 'string', 'max' => 64],
            [['categories', 'platforms', 'models', 'styles'], 'string', 'max' => 32],
            [['website', 'remark'], 'string', 'max' => 255],
            [['refer_to'], 'string', 'max' => 1024],
        ];
    }
    /**
     * 字段名称
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'title' => '标题',
            'employer_id' => '雇主',
            'type' => '雇佣类型',
            'categories' => '分类',
            'platforms' => '上线平台',
            'models' => '设计类型',
            'styles' => '设计风格',
            'website' => '网站',
            'refer_to' => '参考网站',
            'budget' => '预算',
            'view' => '查看次数',
            'collection' => '收藏数量',
            'online' => '上线状态',
            'deleted_at' => '删除时间',
            'status' => '状态',
            'remark' => '备注',
        ];
    }

    /**
     * update & insert data check config for html
     * @param $type string 操作类型
     * @param $encodeJson boolean 是否转成JSON字符串
     * @return string | array
     */
    public static function flyer($type = 'update')
    {
        // jsut search
        $rule = [
            'param' => [
                'title' => ['标题', ['maxlength' => 64, 'required']],
                'type' => ['雇佣类型', ['inkey' => static::$typeSelector, 'required']],
                'categories' => ['分类', ['int', 'required']],
                'platforms' => ['上线平台', ['int']],
                'models' => ['设计类型', ['int', 'required']],
                'styles' => ['设计风格', ['maxlength' => 4, 'required']],
                'website' => ['上线网站', ['url', 'required']],
                'refer_to' => ['参考网站', ['url']],
                'online' => ['上线状态', ['inkey' => static::$onlineSelector, 'required']],
                'budget' => ['预算', ['inkey' => static::$budgetSelector, 'required']],
                'remark' => ['备注', ['maxlength' => 255]],
            ],
        ];
        return $rule;
    }

    /**
     * 获取user信息
     * @return \yii\db\ActiveQuery
     */
    public function getApplicants()
    {
        return $this->hasMany(RecruitApplicant::className(), ['recruit_id' => 'id'])->with('user');
    }

    /**
     * 获取employer信息
     * @return \yii\db\ActiveQuery
     */
    public function getEmployer()
    {
        return $this->hasOne(User::className(), ['id' => 'employer_id']);
    }

    /**
     * 设计师投递简历
     * @return boolean|object
     */
    public function delivery()
    {
        $params = [
            'recruit_id' => $this->id,
            'designer_id' => Yii::$app->user->id,
        ];
        return RecruitApplicant::creator($params);
    }

    /**
     * 增加浏览次数
     * @return boolean
     */
    public function browser()
    {
        $this->view = $this->view + 1;
        return $this->save();
    }

    /**
     * 增加收藏次数
     * @return boolean
     */
    public function collection($is = true)
    {
        $this->collection = $this->collection + ($is ? 1 : -1);
        if($this->collection < 0) {
            $this->collection = 0;
        }
        return $this->save();
    }
}